<% include ../common/header.ejs %>
<body class="layui-layout-body">
<div class="layui-layout layui-layout-admin">
    <% include nav.ejs %>

    <div class="layui-body">
        <!-- 内容主体区域 -->
        <div style="padding: 15px;">
            <fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
                <legend>任务管理</legend>
            </fieldset>

            <table id="demo" lay-filter="demo"></table>
            <script type="text/html" id="barDemo">
                <a  class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del" >删除任务</a>
            </script>
        </div>
    </div>

    <div class="layui-footer">
        <!-- 底部固定区域 -->
        © layui.com - 底部固定区域
    </div>
</div>
<script>
    //JavaScript代码区域
    layui.use(['element','table'], function() {
        var table = layui.table;

        table.render({
            elem:'#demo',
            url:'/admin/task/all',
            method:'post',
            cols:[
                [
                    {field:'title',title:'标题'},
                    {field:'author',title:'作者',templet: (d) => {
                            // console.log(d);
                            return d.author.username
                        }},
                    {field:'time',title:'发布时间',templet: (d) => {
                            return new Date(d.time).toLocaleString()
                        }},
                    {toolbar:'#barDemo'}
                ]
            ]
        })

        table.on('tool(demo)', (obj) => {
            //console.log(obj.data.author._id);
            layer.confirm('是否删除?', {icon: 3, title:'提示'}, function(index){
                //do something
                layui.$.ajax({
                    url:'/admin/task/del',
                    method:'post',
                    data:{
                        _id:obj.data._id,
                        user_id:obj.data.author._id
                    }
                })
                layer.close(index);
            });
        })
    });
</script>

</body>
</html>
